home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / WASTE 1.3a5 / Demo / Source / WEDemoEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-11  |  10.2 KB  |  543 lines  |  [TEXT/CWIE]

  1. /*
  2.     WASTE Demo Project:
  3.     Events Handling
  4.  
  5.     Copyright © 1993-1997 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __APPLEEVENTS__
  12. #include <AppleEvents.h>
  13. #endif
  14.  
  15. #ifndef __AEREGISTRY__
  16. #include <AERegistry.h>
  17. #endif
  18.  
  19. #ifndef __DISKINIT__
  20. #include <DiskInit.h>
  21. #endif
  22.  
  23. #ifndef __TEXTSERVICES__
  24. #include <TextServices.h>
  25. #endif
  26.  
  27. #ifndef __WEDEMOAPP__
  28. #include "WEDemoIntf.h"
  29. #endif
  30.  
  31. static UInt32        sSleepTime = 0;            // sleep time for WaitNextEvent()
  32. static RgnHandle    sMouseRgn = nil;        // mouse region for WaitNextEvent()
  33.  
  34.  
  35. void AdjustCursor ( Point mouseLoc, RgnHandle mouseRgn )
  36. {
  37.     WindowRef window ;
  38.  
  39.     // by default, set mouseRgn to the whole QuickDraw coordinate plane,
  40.     // so that we never get mouse moved events
  41.  
  42.     SetRectRgn ( mouseRgn, -SHRT_MAX, -SHRT_MAX, SHRT_MAX, SHRT_MAX ) ;
  43.  
  44.     // give text services a chance to set the cursor shape
  45.  
  46.     if ( gHasTextServices )
  47.     {
  48.         if ( SetTSMCursor( mouseLoc ) )
  49.         {
  50.             return ;
  51.         }
  52.     }
  53.  
  54.     // if there is a window open, give WEAdjustCursor an opportunity to set the cursor
  55.     // WEAdjustCursor intersects mouseRgn (if supplied) with a region within which
  56.     // the cursor is to retain its shape
  57.     // (if the cursor is outside the view region, this is subtracted from mouseRgn )
  58.  
  59.     if ( ( window = FrontWindow ( ) ) != nil )
  60.     {
  61.         if ( WEAdjustCursor ( mouseLoc, mouseRgn, GetWindowWE ( window ) ) )
  62.         {
  63.             return ;
  64.         }
  65.     }
  66.  
  67.     // set the cursor to the arrow cursor
  68.  
  69.     SetCursor( & qd . arrow ) ;
  70. }
  71.  
  72. void DoMouseDown ( const EventRecord *event )
  73. {
  74.     WindowRef window;
  75.     SInt16 partCode;
  76.  
  77.     // find out where this click when down in
  78.  
  79.     partCode = FindWindow( event->where, &window );
  80.  
  81.     // dispatch on partCode
  82.  
  83.     switch ( partCode )
  84.     {
  85.         case inMenuBar :
  86.         {
  87.             PrepareMenus ( ) ;
  88.             DoMenuChoice ( MenuSelect ( event -> where ), event -> modifiers ) ;
  89.             break;
  90.         }
  91.  
  92.         case inSysWindow:
  93.         {
  94. #if defined ( UNIVERSAL_INTERFACES_VERSION ) && ( UNIVERSAL_INTERFACES_VERSION >= 0x211 )
  95.             SystemClick ( event, window ) ;
  96. #else
  97.             //    Universal Headers prior to version 2.1.2 define SystemClick
  98.             //    incorrectly if STRICT_WINDOWS == 1
  99.             SystemClick ( event, ( WindowPtr ) window ) ;
  100. #endif
  101.             break ;
  102.         }
  103.  
  104.         case inContent:
  105.         {
  106.             if ( DoContent ( event -> where, event, window ) )
  107.             {
  108.                 SelectWindow ( window ) ;
  109.             }
  110.             break;
  111.         }
  112.  
  113.         case inDrag :
  114.         {
  115.             DoDrag ( event -> where, window ) ;
  116.             break;
  117.         }
  118.  
  119.         case inGrow:
  120.         {
  121.             DoGrow ( event -> where, window ) ;
  122.             break;
  123.         }
  124.  
  125.         case inGoAway:
  126.         {
  127.             if ( TrackGoAway ( window, event -> where ) )
  128.             {
  129.                 DoClose( closingWindow, savingAsk, window ) ;
  130.             }
  131.             break;
  132.         }
  133.  
  134.         case inZoomIn:
  135.         case inZoomOut:
  136.         {
  137.             if ( TrackBox ( window, event -> where, partCode ) )
  138.             {
  139.                 DoZoom ( partCode, window ) ;
  140.             }
  141.             break;
  142.         }
  143.     }    // switch
  144. }
  145.  
  146. void DoKeyDown ( const EventRecord *event )
  147. {
  148.     SInt16 key ;
  149.     Boolean isCmdKey ;
  150.  
  151.     // extract character code from event message
  152.  
  153.     key = ( event -> message & charCodeMask );
  154.  
  155.     // is this a command+key combo?
  156.  
  157.     isCmdKey = ( ( event -> modifiers & cmdKey ) != 0 );
  158.  
  159.     // map function keys to the equivalent command+key combos
  160.     // note that all fuction keys generate the same code, i.e. 0x10
  161.  
  162.     if ( key == 0x10 )
  163.     {
  164.         isCmdKey = true ;
  165.  
  166.         switch ( ( event -> message & keyCodeMask ) >> 8 )
  167.         {
  168.             case keyF1:
  169.             {
  170.                 key = 'z' ;
  171.                 break ;
  172.             }
  173.  
  174.             case keyF2:
  175.             {
  176.                 key = 'x' ;
  177.                 break ;
  178.             }
  179.  
  180.             case keyF3:
  181.             {
  182.                 key = 'c' ;
  183.                 break ;
  184.             }
  185.  
  186.             case keyF4:
  187.             {
  188.                 key = 'v' ;
  189.                 break ;
  190.             }
  191.  
  192.             default:
  193.             {
  194.                 key = 0 ;
  195.                 break ;
  196.             }
  197.         }    // switch
  198.     }    // if
  199.  
  200.     // command + printable character combos are routed to MenuKey( )
  201.     // but be sure to pass command + arrow key combos to WEKey( )
  202.  
  203.     if ( isCmdKey && (key >= 0x20 ) )
  204.     {
  205.         PrepareMenus ( );
  206.         DoMenuChoice ( MenuKey ( key ), event -> modifiers ) ;
  207.     }
  208.     else
  209.     {
  210.         DoKey ( key, event ) ;
  211.     }
  212. }
  213.  
  214. void DoDiskEvent ( const EventRecord * event )
  215. {
  216.     Point dialogCorner ;
  217.  
  218.     if ( ( event -> message >> 16 ) != noErr )
  219.     {
  220.         SetPt ( & dialogCorner, 112, 80 ) ;
  221.         DIBadMount ( dialogCorner, event -> message ) ;
  222.     }
  223. }
  224.  
  225. void DoOSEvent ( const EventRecord * event )
  226. {
  227.     SInt16        osMessage ;
  228.     WindowRef    window ;
  229.  
  230.     // extract the OS message field from the event record
  231.  
  232.     osMessage = ( event -> message & osEvtMessageMask ) >> 24 ;
  233.  
  234.     // dispatch on osMessage
  235.  
  236.     switch ( osMessage )
  237.     {
  238.         case suspendResumeMessage:
  239.         {
  240.             if ( ( window = FrontWindow( ) ) != nil )
  241.             {
  242.                 DoActivate( (event->message & resumeFlag) != 0, window );
  243.             }
  244.             break;
  245.         }
  246.  
  247.         case mouseMovedMessage:
  248.         {
  249.             // nothing
  250.             break;
  251.         }
  252.     }
  253. }
  254.  
  255. void DoHighLevelEvent( const EventRecord *event )
  256. {
  257.     AEProcessAppleEvent( event );
  258. }
  259.  
  260. void DoNullEvent( const EventRecord *event )
  261. {
  262. #pragma unused (event)
  263.  
  264.     WindowRef window;
  265.  
  266.     if ( ( window = FrontWindow( ) ) != nil )
  267.     {
  268.         WEIdle( &sSleepTime, GetWindowWE(window) );
  269.     }
  270.     else
  271.     {
  272.         sSleepTime = LONG_MAX;
  273.     }
  274. }
  275.  
  276. void DoWindowEvent( const EventRecord *event )
  277. {
  278.     WindowRef window;
  279.  
  280.     // the message field of the event record contains the window reference
  281.  
  282.     window = (WindowRef) event->message;
  283.  
  284.     // make sure this window is an application window; check the windowKind field
  285.  
  286.     if ( GetWindowKind( window ) != userKind )
  287.         return;
  288.  
  289.     switch ( event->what )
  290.     {
  291.         case updateEvt:
  292.         {
  293.             DoUpdate( window );
  294.             break;
  295.         }
  296.  
  297.         case activateEvt:
  298.         {
  299.             DoActivate( ( event->modifiers & activeFlag) != 0, window );
  300.             break;
  301.         }
  302.     }
  303. }
  304.  
  305. void ProcessEvent( void )
  306. {
  307.     EventRecord event;
  308.     Boolean gotEvent;
  309.  
  310.     gotEvent = WaitNextEvent( everyEvent, &event, sSleepTime, sMouseRgn );
  311.  
  312.     // give text services a chance to intercept this event
  313.     // if TSMEvent( ) handles the event, it will set event.what to nullEvent
  314.  
  315.     if ( gHasTextServices )
  316.     {
  317.         TSMEvent( &event );
  318.     }
  319.  
  320.     // adjust cursor shape and set mouse region
  321.     // (we assume event.where is the current mouse position in global coordinates
  322.     // if event.what <= osEvt; high-level events store the event ID there)
  323.  
  324.     if ( event.what <= osEvt )
  325.     {
  326.         AdjustCursor( event.where, sMouseRgn );
  327.     }
  328.  
  329.     // dispatch on event.what
  330.  
  331.     switch( event.what )
  332.     {
  333.         case nullEvent:
  334.         {
  335.             DoNullEvent( &event );
  336.             break;
  337.         }
  338.  
  339.         case mouseDown:
  340.         {
  341.             DoMouseDown( &event);
  342.             break;
  343.         }
  344.  
  345.         case keyDown:
  346.         case autoKey:
  347.         {
  348.             DoKeyDown( &event );
  349.             break;
  350.         }
  351.  
  352.         case updateEvt:
  353.         case activateEvt:
  354.         {
  355.             DoWindowEvent( &event );
  356.             break;
  357.         }
  358.  
  359.         case diskEvt:
  360.         {
  361.             DoDiskEvent( &event );
  362.             break;
  363.         }
  364.  
  365.         case osEvt:
  366.         {
  367.             DoOSEvent( &event );
  368.             break;
  369.         }
  370.  
  371.         case kHighLevelEvent:
  372.         {
  373.             DoHighLevelEvent( &event );
  374.             break;
  375.         }
  376.     }    // switch
  377.  
  378.     if ( gotEvent )
  379.     {
  380.         sSleepTime = 0;  // force early idle after non-idle event
  381.     }
  382. }
  383.  
  384. OSErr GotRequiredParams( const AppleEvent *ae )
  385. {
  386.     DescType actualType;
  387.     Size actualSize;
  388.     OSErr err;
  389.  
  390.     err = AEGetAttributePtr( ae, keyMissedKeywordAttr, typeWildCard, &actualType, nil, 0, &actualSize );
  391.  
  392.     return    ( err == errAEDescNotFound ) ? noErr :
  393.             ( err == noErr ) ? errAEParamMissed : err;
  394. }
  395.  
  396. static pascal OSErr    HandleOpenDocument( const AppleEvent *ae, AppleEvent *reply, SInt32 refCon )
  397. {
  398. #pragma unused ( reply, refCon )
  399.     AEDescList        docList;
  400.     AEKeyword        keyword;
  401.     DescType        actualType;
  402.     Size            actualSize;
  403.     SInt32            numberOfDocuments, i;
  404.     FSSpec            fileSpec;
  405.     OSErr            err;
  406.  
  407.     docList.descriptorType = typeNull;
  408.     docList.dataHandle = nil;
  409.  
  410.     // extract direct parameter from the Apple event
  411.  
  412.     if ( ( err = AEGetParamDesc( ae, keyDirectObject, typeAEList, &docList ) ) != noErr )
  413.         goto cleanup;
  414.  
  415.     // perform the recommended check for additional required parameters
  416.  
  417.     if ( ( err = GotRequiredParams( ae ) ) != noErr )
  418.         goto cleanup;
  419.  
  420.     // count the items in the list of aliases
  421.  
  422.     if ( ( err = AECountItems( &docList, &numberOfDocuments ) ) != noErr )
  423.         goto cleanup;
  424.  
  425.     for ( i = 1; i <= numberOfDocuments; i++ )
  426.     {
  427.         // coerce the nth alias to a file system specification record
  428.  
  429.         if ( ( err = AEGetNthPtr( &docList, i, typeFSS, &keyword, &actualType,
  430.                 &fileSpec, sizeof( fileSpec ), &actualSize ) ) != noErr )
  431.             goto cleanup;
  432.  
  433.         // open the specified file
  434.  
  435.         if ( ( err = CreateWindow( &fileSpec ) ) != noErr )
  436.             goto cleanup;
  437.     }
  438.  
  439. cleanup:
  440.     AEDisposeDesc( &docList );
  441.     return err;
  442. }
  443.  
  444. static pascal OSErr    HandleOpenApplication( const AppleEvent *ae, AppleEvent *reply, SInt32 refCon )
  445. {
  446. #pragma unused ( reply, refCon )
  447.     OSErr        err;
  448.  
  449.     // perform the recommended check for additional required parameters
  450.  
  451.     if ( ( err = GotRequiredParams( ae ) ) != noErr )
  452.         goto cleanup;
  453.  
  454.     // create a new window from scratch
  455.  
  456.     err = CreateWindow( nil );
  457.  
  458. cleanup:
  459.     return err;
  460. }
  461.  
  462. static pascal OSErr    HandleQuitApplication( const AppleEvent *ae, AppleEvent *reply, SInt32 refCon )
  463. {
  464. #pragma unused (reply, refCon)
  465.  
  466.     AEKeyword        optKey;
  467.     DescType        actualType;
  468.     Size            actualSize;
  469.     SavingOption    saving;
  470.     OSErr            err;
  471.  
  472.     // default saving option is savingAsk;
  473.  
  474.     saving = savingAsk;
  475.  
  476.     // extract optional save options
  477.  
  478.     if ( ( err = AEGetParamPtr( ae, keyAESaveOptions, typeEnumerated, &actualType, &optKey, sizeof( optKey ), &actualSize ) ) == noErr )
  479.     {
  480.         if ( optKey == kAEYes )
  481.         {
  482.             saving = savingYes;
  483.         }
  484.         else if (optKey == kAENo )
  485.         {
  486.             saving = savingNo;
  487.         }
  488.         else if ( optKey != kAEAsk )
  489.         {
  490.             err = paramErr;    // for want of a better code
  491.             goto cleanup;
  492.         }
  493.     }
  494.  
  495.     // perform the recommended check for additional required parameters
  496.  
  497.     if ( ( err = GotRequiredParams( ae ) ) != noErr )
  498.         goto cleanup;
  499.  
  500.     // actually do the quit stuff
  501.  
  502.     err = DoQuit( saving );
  503.  
  504. cleanup:
  505.     return err;
  506. }
  507.  
  508. OSErr InitializeEvents( void )
  509. {
  510.     OSErr    err;
  511.  
  512.     // allocate space for the mouse region
  513.  
  514.     sMouseRgn = NewRgn( );
  515.  
  516.     // install AppleEvent handlers for the Required Suite
  517.  
  518.     if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc( HandleOpenApplication ), 0, false ) ) != noErr )
  519.         goto cleanup;
  520.  
  521.     if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc( HandleOpenDocument ), kDoOpen, false ) ) != noErr )
  522.         goto cleanup;
  523.  
  524.     if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc( HandleOpenDocument ), kDoPrint, false ) ) != noErr )
  525.         goto cleanup;
  526.  
  527.     if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc( HandleQuitApplication ), 0, false ) ) != noErr )
  528.         goto cleanup;
  529.  
  530.     // install Apple event handlers for a subset of the Core suite
  531.  
  532.     if ( ( err = InstallCoreHandlers( ) ) != noErr )
  533.         goto cleanup;
  534.  
  535.     // install Apple event handlers for inline input
  536.  
  537.     if ( ( err = WEInstallTSMHandlers( ) ) != noErr )
  538.         goto cleanup;
  539.  
  540. cleanup:
  541.     return err;
  542. }
  543.